package com.kj.platformlogistics.home.index;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.kj.platformlogistics.R;
import com.kj.platformlogistics.base.BaseVPFragment;
import com.kj.platformlogistics.common.MethodUtils;
import com.kj.platformlogistics.common.UserData;
import com.kj.platformlogistics.constants.UrlConstants;
import com.kj.platformlogistics.home.activity.AllHistoryOrderListActivity;
import com.kj.platformlogistics.home.activity.FinishActivity;
import com.kj.platformlogistics.home.activity.SearchOrderListActivity;
import com.kj.platformlogistics.home.activity.SendingActivity;
import com.kj.platformlogistics.home.activity.SolicilationedActivity;
import com.kj.platformlogistics.home.activity.StockListActivity;
import com.kj.platformlogistics.home.activity.WaitSendActivity;
import com.kj.platformlogistics.home.activity.WaitSolicilationActivity;
import com.kj.platformlogistics.home.adapter.HomeHistoryOrderAdapter;
import com.kj.platformlogistics.home.model.HomeHistoryOrderList;
import com.kj.platformlogistics.http.HttpRequest;
import com.kj.platformlogistics.http.HttpStringCallBack;
import com.kj.platformlogistics.systemnotification.SystemNotification;
import com.kj.platformlogistics.util.JsonUtils;
import com.stx.xhb.xbanner.XBanner;

import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class HomeIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private View view;
    private RecyclerView rcCommon;
    private List<Integer> banners;
     private HomeHistoryOrderAdapter adapter;
    private XBanner banner;
    private ImageView ivMsg;
    private TextView tvCheckAll,tvRole;
    private LinearLayout llWaitSolicilation,llWaitSend,
                        llSending, llSolicilationed,llFinished,
                        llStock, llSearchOrder;

    private LinearLayout llDriver;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
           case  R.id.ivMsg:
            SystemNotification.actionStart(context);
            break;
            case R.id.llWaitSolicilation:
                WaitSolicilationActivity.actionStart(context);
                break;
            case R.id.llWaitSend:
                WaitSendActivity.actionStart(context);
                break;
            case R.id.llSending:
                SendingActivity.actionStart(context);
                break;
            case R.id.llSolicilationed:
                SolicilationedActivity.actionStart(context);
                break;
            case R.id.llFinished:
                FinishActivity.actionStart(context);
                break;
            case R.id.llStock:
                //库存列表
                StockListActivity.actionStart(context);
                break;
            case R.id.tvCheckAll:
                //查看全部历史订单
                AllHistoryOrderListActivity.actionStart(context);
                break;
            case R.id.llSearchOrder:
                SearchOrderListActivity.actionStart(context);
                break;
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible){
            if (UserData.create(context).get(UserData.ROLE)==null){
                UserData.create(context).changeUserData(UserData.ROLE,"0");
            }
            if (UserData.create(context).get(UserData.ROLE).equals("0")){
                //司机端
                tvRole.setText("司机端");
                llWaitSolicilation.setVisibility(View.VISIBLE);
                llStock.setVisibility(View.GONE);
                llDriver.setVisibility(View.VISIBLE);
                llSolicilationed.setVisibility(View.VISIBLE);
            }else {
                //分拣员端
                tvRole.setText("分拣员端");
                llDriver.setVisibility(View.GONE);
                llStock.setVisibility(View.VISIBLE);
                llWaitSolicilation.setVisibility(View.GONE);
                llSolicilationed.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void onFragmentFirstVisible() {
        getHistoryData();
    }

    public static HomeIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        HomeIndex fl = new HomeIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.home_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        banner=view.findViewById(R.id.banner);
        llSearchOrder=view.findViewById(R.id.llSearchOrder);
        ivMsg=view.findViewById(R.id.ivMsg);
        rcCommon=view.findViewById(R.id.rcCommon);
        llDriver=view.findViewById(R.id.llDriver);
        tvCheckAll=view.findViewById(R.id.tvCheckAll);
        llWaitSend=view.findViewById(R.id.llWaitSend);
        llSending=view.findViewById(R.id.llSending);
        llWaitSolicilation=view.findViewById(R.id.llWaitSolicilation);
        llSolicilationed=view.findViewById(R.id.llSolicilationed);
        llFinished=view.findViewById(R.id.llFinished);
        llStock=view.findViewById(R.id.llStock);
        tvRole=view.findViewById(R.id.tvRole);
        llFinished.setOnClickListener(this::onClick);
        llSolicilationed.setOnClickListener(this::onClick);
        ivMsg.setOnClickListener(this::onClick);
        llSearchOrder.setOnClickListener(this::onClick);
        tvCheckAll.setOnClickListener(this::onClick);
        llWaitSend.setOnClickListener(this::onClick);
        llWaitSolicilation.setOnClickListener(this::onClick);
        llSending.setOnClickListener(this::onClick);
        llStock.setOnClickListener(this::onClick);
        banners=new ArrayList<>();
        banners.add(R.mipmap.icon_home_default_banner);
        banners.add(R.mipmap.icon_home_default_banner);
        banners.add(R.mipmap.icon_home_default_banner);
        banners.add(R.mipmap.icon_home_default_banner);




        banner.setData(R.layout.item_banner1,banners,null);
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object object, View view, int position) {
                RelativeLayout container=(RelativeLayout) view;
                ImageView ivBanner=container.findViewById(R.id.ivBanner);
                Glide.with(context).load(banners.get(position)).into(ivBanner);
            }
        });

        banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object obj, View view, int position) { }
        });
        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });


    }


    private void getHistoryData() {
        new HttpRequest(context).doPost(UrlConstants.logistics_getHistoryOrder, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), HomeHistoryOrderList.class);
                        if (obj instanceof HomeHistoryOrderList) {
                            HomeHistoryOrderList model = (HomeHistoryOrderList) obj;
                            if (model.getResult().size()>0){
                                adapter=new HomeHistoryOrderAdapter(model.getResult());
                                rcCommon.setLayoutManager(new LinearLayoutManager(context));
                                rcCommon.setAdapter(adapter);
                            }
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

}